package com.ali.g308_ai.controller;


import com.ali.g308_ai.service.impl.ChatIdsServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/ai")
@RequiredArgsConstructor
public class ChatController {


    private final ChatClient chatClient;

    private final ChatIdsServiceImpl chatHistoryServiceImpl;

    /**
     *
     * @param prompt 注意提示词名称与前端请求一致
     * @param chatId 用于保存对话
     * @return 流返回ai响应
     */
    @RequestMapping(value = "/chat",produces = "text/html;charset=utf-8")
    public Flux<String> chat(String prompt,String chatId) {
        chatHistoryServiceImpl.saveChatIds("chat",chatId);
        return chatClient.prompt()
                .user(prompt)
                .advisors(advisorSpec -> advisorSpec.param(ChatMemory.CONVERSATION_ID,chatId))
                .stream()
                .content();
    }
}
